﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.Admin == "undefined") Axis.Admin = {};

Axis.Admin.UserGroup = {
    Init: function () {
        var thisObj = this;
        thisObj.Grid.Init();
        thisObj.EventClick();
        thisObj.DropdownChecklist.Init();
        var cmaskdate = new CMaskDate();
        cmaskdate.Init($("input[role=datepicker]"));
    },
    DropdownChecklist: {
        Instance: null,
        Init: function () {
            var thisObj = this;
            thisObj.Instance = new CDropDownCheckList({ width: 200, maxDropHeight: 110, firstItemChecksAll: true });
            thisObj.Instance.Init("#FormEdit select[multiple]");
            $("#FormEdit").hide();
        },
        Clear: function () {
            var thisObj = this;
            $("#FormEdit select[multiple]").find("option").removeAttr("selected");
            thisObj.Refresh();
        },
        Refresh: function () {
            var thisObj = this;
            if (thisObj.Instance) {
                thisObj.Instance.Refresh();
            }
        }
    },
    Grid: {
        Instance: null,
        ParentID: 23,
        ID: "#GridContents",
        PagesID: "#GridPages",
        GridURL: "Service/UserGroupService.ashx?Action=GET&FunctionCode=UserGroup",
        Init: function () {
            var thisObj = this;
            var options = {
                url: thisObj.GridURL,
                datatype: "json",
                colNames: [
                    'ID', 'Code', 'Name', 'Description', 'Action'
                ],
                colModel: [
                    { name: 'ID', index: 'ID', width: 50, hidden: true, frozen: true },
                    { name: 'Code', index: 'Code', width: 100, frozen: true },
                    { name: 'Name', index: 'Name', width: 150, frozen: true },
                    { name: 'Description', index: 'Description', width: 250 },
                    { name: 'Action', index: 'Action', width: 90, align: 'center' }
                ],
                afterInsertRow: function (rowid, aData, addData) {
                 

                },
                onCellSelect: function (rowid, iCol, cellcontent, e) {

                },
                onSelectRow: function (rowid, status, e) {

                },
                ondblClickRow: function (rowid, iRow, iCol, e) {

                },
               // multiselect: true,
                height: 'auto',
                rownumbers: true,
                autowidth: true,
                //forceFit: true,
                shrinkToFit: false,
                rowNum: 20,
                pager: thisObj.PagesID
            };
            thisObj.Instance = new CGrid(options);
            thisObj.Instance.Init(thisObj.ID);

            thisObj.Instance.NavGrid();
            thisObj.Instance.FrozenColumns();
        },
        GetColumnModel: function () {
            var thisObj = this;
            if (thisObj.Instance)
                return thisObj.Instance.GetColumnModel();
            else
                return null
        },
        Reload: function () {
            var thisObj = this;
            if (thisObj.Instance)
                thisObj.Instance.ReloadGrid();
        },
        SelectRow: function () {
            var thisObj = this;
            if (thisObj.Instance)
                return thisObj.Instance.SelectRow();
            else
                return null;
        }
    },
    DialogEdit: {
        ID: "#FormEdit",
        Dialog: null,
        Init: function (action, opts) {
            var thisObj = this;
            thisObj.ClearForm();

            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
            else {
                thisObj.Dialog = new CDialog({
                    title: "Edit User",
                    width: 400,
                    height: 400
                });
                thisObj.Dialog.Init(thisObj.ID);
            }
            var buttonText = "";
            switch (action) {
                case "INSERT":
                    thisObj.Dialog.SetTitle("Add New User");
                    $(thisObj.ID).find("[data-role='INSERT']").show();

                    $(thisObj.ID).find("#txtLoginName").focus();
                    buttonText = "Add";
                    break;
                case "UPDATE":
                    thisObj.Dialog.SetTitle("Edit: " + opts.Name);
                    $.each(opts, function (key, value) {
                        $(thisObj.ID).find("[data-field=" + key + "]").val(value);
                    });
                    Axis.Admin.UserGroup.GetMemberGroupDetail(opts.ID);
                    buttonText = "Update";
                    break;
            }
            var buttons = [
                        {
                            text: buttonText,
                            click: function (e) {
                                Axis.Admin.UserGroup.Update(action, opts);
                                thisObj.Close();
                            }
                        },
                        {
                            text: "Cancel",
                            click: function (e) {
                                thisObj.Close();
                            }
                        }

            ];
            $(thisObj.ID).dialog('option', 'buttons', buttons);
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Close();
            }
        },
        ClearForm: function () {
            $("#FormEdit").find("[data-field]").each(function () {
                var $this = $(this);
                $this.val("");
                if ($this.is("select")) {
                    var value = $this.find("option[Default-Value='1']").attr("value");
                    $this.val(value);
                }

            });
            $("#FormEdit").find("[data-role='INSERT']").hide();
            Axis.Admin.UserGroup.DropdownChecklist.Clear();
        }
    },
    GetMemberGroupDetail: function (memberGroupID) {
        var thisObj = this;
        $.Ajax({
            url: 'Service/UserGroupService.ashx',
            type: 'POST',
            data: {
                Action: "GetCriteria",
                MemberGroupID: memberGroupID
            },
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    data = eval("(" + data + ")");
                    $.each(data, function (index, item) {
                        var CriteriaID = item.CriteriaID;
                        var Value = item.Value;
                        var $selector = $($.string.Format("select[criteriaid='{0}']", CriteriaID));
                        var $option = $selector.find($.string.Format("option[value='{0}']", Value));
                        $option.attr("selected", "selected");
                    });
                    thisObj.DropdownChecklist.Refresh();
                }
            },
            beforeSend: function (jqXHR, settings) {
            },
            error: function (jqXHR, textStatus, errorThrown) {
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });
    },
    Update: function (type, opts) {
        var thisObj = this;
        var data = {};
        $("#FormEdit").find("[data-field]").each(function () {
            var $this = $(this);
            if ($this.attr("data-field")) {
                var key = $this.attr("data-field");
                var value = $this.val();
                if ($.isArray(value)) {
                    value = $.map(value, function (val) {
                        if (val)
                            return val;
                    });
                    value = value.join(";");
                    if (key == "Criteria") {
                        key = "Criteria_" + $this.attr("CriteriaID");
                    }
                }
                
                data[key] = value;
            }
        });
        data.Action = type;
        data.DataCode = "UserGroup";
        $.Ajax({
            url: 'Service/UserGroupService.ashx',
            type: 'POST',
            data: data,
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    data = eval("(" + data + ")");
                    if (data.IsSuccessfull) {
                        thisObj.Grid.Reload();
                        $.CAlert.Alert({
                            Title: "Nhóm Thành Viên",
                            Content: "Cập nhật thành công.",
                        });
                    }
                }
            },
            beforeSend: function (jqXHR, settings) {
            },
            error: function (jqXHR, textStatus, errorThrown) {
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });
    },
    DialogFilter: {
        Init: function () { }
    },
    EventClick: function () {
        var thisObj = this;
        $("#imgUserAdd").click(function () {
            thisObj.DialogEdit.Init("INSERT");
        });
        $("#imgUserEdit").click(function () {
            var rowData = thisObj.Grid.SelectRow();
            if (rowData && rowData.ID)
                thisObj.DialogEdit.Init("UPDATE", rowData);
            else alert("Please Select Row");
        });
        $("#imgUserFilter").click(function () {
            thisObj.DialogFilter.Init();
        });

    }
};
$(function () {
    Axis.Admin.UserGroup.Init();
});
function ShowEdit(rowID) {
    var thisObj = Axis.Admin.UserGroup;
    var rowData = $(thisObj.Grid.ID).jqGrid('getRowData', rowID);
    if (rowData && rowData.ID)
        thisObj.DialogEdit.Init("UPDATE", rowData);
}